﻿#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using BusinessObjects;

#endregion

namespace FastTrackCabService.AdminPanel
{
    public partial class Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Page_PreRenderComplete(object sender, EventArgs e)
        {
            int bookingId;
            if (int.TryParse(tbSearch.Text.Trim(), out bookingId))
            {
                Booking booking = BookingBLL.GetBooking(bookingId);
                gvBookings.DataSource = booking == null ? null : new List<Booking> {booking};
            }
            else
            {
                //Filter by status
                List<Booking> bookings = ddlStatus.SelectedValue == "All"
                    ? BookingBLL.GetAllBookings()
                    : BookingBLL.GetAllBookings(ddlStatus.SelectedValue);

                //Filter by type
                bookings = ddlType.SelectedValue == "All"
                    ? bookings.OrderByDescending(b => b.BookingId).ToList()
                    : bookings.Where(b => b.Type == ddlType.SelectedValue).OrderByDescending(b => b.BookingId).ToList();

                gvBookings.DataSource = bookings;
            }
            gvBookings.DataBind();
        }

        protected void ibtnSearch_OnClick(object sender, ImageClickEventArgs e)
        {
            ddlStatus.SelectedIndex = 0;
            ddlType.SelectedIndex = 0;
        }

        protected void ddlStatus_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            tbSearch.Text = string.Empty;
        }

        protected void gvBookings_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvBookings.PageIndex = e.NewPageIndex;
        }

        protected void ibDelete_OnClick(object sender, ImageClickEventArgs e)
        {
            BookingBLL.ChangeStatus(Convert.ToInt32(Request.QueryString["bookingId"]), "Rejected");
        }
    }
}